package com.example.sc.learn.service.impl;

import com.example.sc.learn.dao.UserDao;
import com.example.sc.learn.entity.User;
import com.example.sc.learn.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * Created by baipan
 * Date: 2019-03-30
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUser(User user) throws Exception {
        User u = userDao.selectByUserCode(user.getUserCode());
        if (u==null || StringUtils.isEmpty(u.getUserCode())){
            user.setId(UUID.randomUUID().toString());
            userDao.insertUser(user);
        }else{
            throw new Exception("此编号已经存在");
        }
    }

    @Override
    @Transactional(readOnly = true)
    public User selectByUserCode(String userCode) {
        return userDao.selectByUserCode(userCode);
    }


}
